package me.uniauto.common.interceptor;

import me.uniauto.Constants.HttpConstants;
import me.uniauto.bean.MUser;
import me.uniauto.util.CommResp;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by hardy on 2018/2/12.
 */
public class CheckVersionInterceptor extends HandlerInterceptorAdapter{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestIp = request.getRequestURI();
        //不验证登陆
        boolean isLogin = false;
        for(String urlIp: HttpConstants._excludeLogin){
            if(requestIp.contains(urlIp)){
                isLogin = true;
            }
        }
        MUser mUserVO = new MUser();
        if(isLogin){
            mUserVO = (MUser) request.getSession().getAttribute(HttpConstants._session);
            if(mUserVO==null){
                response.setContentType(HttpConstants.UTF_8);
                PrintWriter out = response.getWriter();
                out.print(CommResp.user_not_login);
                out.flush();
                return false;
            }else{
                request.setAttribute(HttpConstants._user_vo,mUserVO);
            }
        }
        return true;
    }
}
